package com.cmcc.linlaker.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import java.util.List;

public class SystemUtils {

    private static SystemUtils systemUtils;

    public static SystemUtils getInstance() {
        if (systemUtils != null) {
            return systemUtils;
        } else {
            return new SystemUtils();
        }
    }

    /**
     * 判断系统是否安装某app
     *
     * @param packageName 包名
     * @return
     */
    public boolean appIsInstalled(Context context, String packageName) {
        PackageManager manager = context.getPackageManager();
        List<PackageInfo> packageInfoList = manager.getInstalledPackages(0);
        if (packageInfoList != null) {
            for (int i = 0; i < packageInfoList.size(); i++) {
                String package_name = packageInfoList.get(i).packageName;
                if (package_name.equals(packageName)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 获取某app的信息
     *
     * @param packageName 包名
     * @return
     */
    public PackageInfo getAppInfo(Context context, String packageName) {
        PackageManager manager = context.getPackageManager();
        List<PackageInfo> packageInfoList = manager.getInstalledPackages(0);
        if (packageInfoList != null) {
            for (int i = 0; i < packageInfoList.size(); i++) {
                String package_name = packageInfoList.get(i).packageName;
                if (package_name.equals(packageName)) {
                    return packageInfoList.get(i);
                }
            }
        }
        return null;
    }
}